package com.banciyuan.bcywebview.biz.setting;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.api.AskApi;
import com.banciyuan.bcywebview.base.activity.BaseActivity;
import com.banciyuan.bcywebview.base.helper.ActionbarHelper;
import com.banciyuan.bcywebview.base.helper.ProgressbarHelper;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.base.view.dialog.WaitLightDialog;
import com.banciyuan.bcywebview.biz.ask.AskHelper;
import com.banciyuan.bcywebview.utils.http.BcyPair;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.http.StringRequestParam;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import de.greenrobot.daoexample.model.AskHeaderInfo;


public class AskSettingActivity extends BaseActivity implements View.OnClickListener {
    private ProgressbarHelper mProgressbarHelper;
    private View mProgressbarView;
    private ImageView ivAsk;
    private AskHelper askHelper;
    private WaitLightDialog waitLightDialog;
    private AskHeaderInfo askHeaderInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ask_setting_layout);
        initProgressbar();
        initActionbar();
        initUi();
        initAction();
        initData();
    }


    @Override
    protected void initProgressbar() {
        mProgressbarView = findViewById(R.id.base_progressbar);
        mProgressbarHelper = new ProgressbarHelper(mProgressbarView);
        mProgressbarHelper.setActionCallbacks(new ProgressbarHelper.OnProgressCallbacks() {
            @Override
            public void onActionClick(int id) {
                mProgressbarHelper.onLoading();
                initData();
            }
        });
        mProgressbarHelper.onLoading();
    }

    @Override
    protected void initActionbar() {
        View mActionBarView = findViewById(R.id.base_action_bar);
        ActionbarHelper mActionbarHelper = new ActionbarHelper(this, mActionBarView);
        mActionbarHelper.setTitle(getString(R.string.ask_setting));
    }

    @Override
    protected void initUi() {
        ivAsk = (ImageView) findViewById(R.id.ask_iv);
    }

    @Override
    protected void initAction() {
        ivAsk.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        askHelper = new AskHelper(this);
        askHelper.getHeaderInfo(UserDataHelper.getInstance(this).getUid(), new AskHelper.GetAskdata() {
            @Override
            public void succ(String json) {
                try {
                    Gson gson = new Gson();
                    askHeaderInfo = gson.fromJson(json, AskHeaderInfo.class);
                    updateAskSettingView();
                    mProgressbarHelper.onSuccess();
                } catch (Exception e) {
                    mProgressbarHelper.onFailed();
                }
            }

            @Override
            public void fail(String fail) {
                mProgressbarHelper.onFailed();
            }
        });
    }

    private void updateAskSettingView() {
        if (askHeaderInfo.getAnonymous() == 0) {
            ivAsk.setImageResource(R.drawable.switch_on);
        } else {
            ivAsk.setImageResource(R.drawable.switch_off);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ask_iv:
                waitLightDialog = new WaitLightDialog(getContext(), R.style.BestDialog);
                waitLightDialog.show();
                doAskSet(1 - askHeaderInfo.getAnonymous());
                break;
        }
    }

    private void doAskSet(final int is_anonymous) {
        String url = HttpUtils.BASE_URL + AskApi.getDosaveset();
        List<BcyPair> pairs = new ArrayList<>();
        pairs.add(new BcyPair(HttpUtils.TOKEN, UserDataHelper.getInstance(this).getToken()));
        pairs.add(new BcyPair(HttpUtils.ASK_ANONYMOUS, String.valueOf(is_anonymous)));
        Map<String, String> map = HttpUtils.getData(pairs);
        Response.Listener<String> onSucc = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (StatusCodeUtils.dealStatusCode(response, AskSettingActivity.this)) {
                    askHeaderInfo.setAnonymous(is_anonymous);
                    updateAskSettingView();
                    waitLightDialog.dismiss();
                } else {
                    waitLightDialog.dismiss();
                }
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                waitLightDialog.dismiss();
            }
        };

        StringRequestParam param = new StringRequestParam(Request.Method.POST, url, map, onSucc, onError);
        VolleyQueue.getRquest(this).add(param);
    }
}
